﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using Elderos.Dialogs;
using Microsoft.Samples.CustomControls;
using TreeBuilder.Settings;

namespace TreeBuilder
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            AppDomain.CurrentDomain.UnhandledException += HandleException;
            new FontChooser();
        }

        private static void HandleException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = (Exception) e.ExceptionObject;
            MsgBox.Show(ex.ToString(), "Unhandled exception", null,MsgBoxButtons.Ok, MsgBoxImages.Stop);
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            if (e.Args.Length <= 0)
            {
                MainWindow = CreateMainWindow();
                MainWindow.Show();
            }
            else
            {
                string filename = e.Args[0];

                var window = CreateMainWindow();
                MainWindow = window;
                window.OpenFromFile(filename);

                MainWindow.Show();
            }

            if (ApplicationSettings.Default.FirstTime)
            {
                ShowFirstTimeTips();
                ApplicationSettings.Default.FirstTime = false;
                ApplicationSettings.Default.Save();
                return;
            }

            Task.Factory.StartNew(CheckUpdate);
        }


        private void ShowFirstTimeTips()
        {
            var window = ((MainWindow)MainWindow);

            window.CreateNewTree();

            var tipsBorder = window.FirstTimeTipsBorder;
            tipsBorder.Visibility = Visibility.Visible;

            Grid.SetRowSpan(window.TabControl, 1);

            var animation = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(1)));
            tipsBorder.BeginAnimation(UIElement.OpacityProperty, animation);
        }

        private void CheckUpdate()
        {
            var webclient = new WebClient();
            try
            {
                var version = Version.Parse(webclient.DownloadString("http://treebuilder.ru/Home/Version"));
                var appversion = Assembly.GetExecutingAssembly().GetName().Version;
                if (appversion >= version)
                    return;
            }
            catch (Exception)
            {
                return;
            }

            Dispatcher.Invoke(new Action(ShowUpdateNotification));
        }

        private void ShowUpdateNotification()
        {
            var window = ((MainWindow) MainWindow);
            var notificationBorder = window.UpdateNotificationBorder;
            notificationBorder.Visibility = Visibility.Visible;

            Grid.SetRowSpan(window.TabControl, 1);

            var animation = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(1)));
            notificationBorder.BeginAnimation(UIElement.OpacityProperty, animation);
        }

        private MainWindow CreateMainWindow()
        {
            var window = new MainWindow();
            window.Closed += (o, e) => Application.Current.Shutdown();
            return window;
        }
    }
}
